package com.dog
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest
class ImgController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [imgInstanceList: Img.list(params), imgInstanceTotal: Img.count()]
    }

    def create = {
        def imgInstance = new Img()
        imgInstance.properties = params
        return [imgInstance: imgInstance]
    }

    def save = {
        def imgInstance = new Img(params)
		DefaultMultipartHttpServletRequest muliRequest = (DefaultMultipartHttpServletRequest)request
		def iter= muliRequest.getFileNames()
		while(iter.hasNext()){
			def name = iter.next()
			def f = request.getFile(name)
			def fileName = f.originalFilename
			def fileID=new Random().nextInt(1000000);
			def saveFileName=fileID+"${fileName[fileName.lastIndexOf('.')..-1]}"
			if(!f.empty){
				f.transferTo(new File(servletContext.getRealPath("/upload"),saveFileName))
			}
			imgInstance.location = "/dogShow/upload/$saveFileName"
		}	
        if (imgInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'img.label', default: 'Img'), imgInstance.id])}"
            redirect(action: "show", id: imgInstance.id)
        }
        else {
            render(view: "create", model: [imgInstance: imgInstance])
        }
    }

    def show = {
        def imgInstance = Img.get(params.id)
        if (!imgInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
            redirect(action: "list")
        }
        else {
            [imgInstance: imgInstance]
        }
    }

    def edit = {
        def imgInstance = Img.get(params.id)
        if (!imgInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [imgInstance: imgInstance]
        }
    }

    def update = {
        def imgInstance = Img.get(params.id)
        if (imgInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (imgInstance.version > version) {   
                    imgInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'img.label', default: 'Img')] as Object[], "Another user has updated this Img while you were editing")
                    render(view: "edit", model: [imgInstance: imgInstance])
                    return
                }
            }
            imgInstance.properties = params
			DefaultMultipartHttpServletRequest muliRequest = (DefaultMultipartHttpServletRequest)request
			def iter= muliRequest.getFileNames()
			while(iter.hasNext()){
				def name = iter.next()
				def f = request.getFile(name)
				def fileName = f.originalFilename
				def fileID=new Random().nextInt(1000000);
				def saveFileName=fileID+"${fileName[fileName.lastIndexOf('.')..-1]}"
				if(!f.empty){
					f.transferTo(new File(servletContext.getRealPath("/upload"),saveFileName))
				}
				imgInstance.location = "/upload/$saveFileName"
			}
            if (!imgInstance.hasErrors() && imgInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'img.label', default: 'Img'), imgInstance.id])}"
                redirect(action: "show", id: imgInstance.id)
            }
            else {
                render(view: "edit", model: [imgInstance: imgInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def imgInstance = Img.get(params.id)
        if (imgInstance) {
            try {
                imgInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'img.label', default: 'Img'), params.id])}"
            redirect(action: "list")
        }
    }
}
